package com.geline.sqlbackup.service;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;

import java.io.File;
import java.io.IOException;

/**
 * @author gongyuxiang
 * @date 2021-04-05 19:33:40
 */
@Deprecated
public class ShenTongBackupUtil {

//    @Value("${backup.userName}")
//    private String userName;
//    @Value("${backup.password}")
//    private String password;
//    @Value("${backup.savePath}")
//    private String savePath;
//    @Value("${backup.fileName}")
//    private String fileName;
//    @Value("${backup.databaseName}")
//    private String databaseName;
//    @Value("${backup.hostIP}")
//    private String hostIP;
    private DataSourceProperties dataSource;

    public boolean backup(String hostIP, String userName, String password, String savePath, String fileName, String databaseName) {
        System.out.println(savePath);
        File saveFile = new File(savePath);
        File s = new File("/opt/eq.sql");
        if (!saveFile.exists()) {// 如果目录不存在
            s.mkdirs();// 创建文件夹

        }
        if (!savePath.endsWith(File.separator)) {
            savePath = savePath + File.separator;
        }

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("mysqldump").append(" -opt").append(" -h").append(hostIP);
        stringBuilder.append(" -u").append(userName).append(" -p").append(password).append(" -lock-all-tables=true");
        stringBuilder.append(" -result-file=").append("/opt/eq.sql").append(" -default-character-set=utf8 ").append(databaseName);
        try {
            System.out.println(stringBuilder.toString());
            Process process = Runtime.getRuntime().exec(stringBuilder.toString());
            if (process.waitFor() == 0) {// 0 表示线程正常终止。
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;

    }


}
